<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title id="minna-title"></title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport"
    content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="./layui-minna/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="./layui-minna/minna.css" media="all">
  <style>

  </style>
  <script>
  </script>
</head>
<body class="layui-layout-body">
  <div id="CONTAINER_MINNA">
    <div class="layui-layout layui-layout-minna">
      <div class="layui-header">
        <ul class="layui-nav layui-layout-left">
          <li class="layui-nav-item layminna-flexible" lay-unselect>
            <a href="javascript:;" layminna-event="flexible" title="侧边伸缩">
              <i class="layui-icon layui-icon-shrink-right" id="LAY_app_flexible"></i>
            </a>
          </li>
          <li class="layui-nav-item layui-hide-xs" lay-unselect>
            <a href="" target="_blank" title="首页">
              <i class="layui-icon layui-icon-website"></i>
            </a>
          </li>
          <li class="layui-nav-item" lay-unselect>
            <a href="javascript:;" layminna-event="refresh" title="刷新">
              <i class="layui-icon layui-icon-refresh-3"></i>
            </a>
          </li>
          <li class="layui-nav-item layui-hide-xs" lay-unselect>
            <a href="javascript:;" layminna-event="fullscreen">
              <i class="layui-icon layui-icon-screen-full"></i>
            </a>
          </li>
        </ul>
        <ul class="layui-nav layui-layout-right" lay-filter="layminna-layout-right">
          <li class="layui-nav-item" lay-unselect>
            <a href="javascript:;">
                <cite name="omt-nickname">未登录</cite>
            </a>
            <dl class="layui-nav-child">
                <dd><a lay-href="system/info.html" style="text-align: center;">基本资料</a></dd>
                <hr>
                <dd layminna-event="logout" style="text-align: center;"><a>退出</a></dd>
            </dl>
          </li>
        </ul>
      </div>

      <div class="layui-side layui-side-menu">
        <div class="layui-side-scroll">
          <div id="minna-homeurl-sidmenulogo" class="layui-logo" lay-href="">
            <a id="minna-icon" href="javascript:;"><img src="./home-minna/icon.png" class="layui-nav-img"></a>
            <span id="minna-headtitle"></span>
          </div>
          <ul id = "cata" class="layui-nav layui-nav-tree" lay-shrink="all" id="LAY-system-side-menu" lay-filter="layminna-system-side-menu">         
          </ul>   
        </div>
      </div>

      <!-- 页面标签 -->
      <div class="layminna-pagetabs" id="LAY_minna_tabs">
        <div class="layui-icon layminna-tabs-control layui-icon-prev" id="window-comeback"></div>
         
        <div class="layui-icon layminna-tabs-control layui-icon-next" id="window-gonext"></div>
        <div class="layui-icon layminna-tabs-control layui-icon-down">
          <ul class="layui-nav layminna-tabs-select" lay-filter="layminna-pagetabs-nav">
            <li class="layui-nav-item" lay-unselect>
              <a href="javascript:;"></a>
              <dl class="layui-nav-child layui-anim-fadein">
                <dd layminna-event="closeThisTabs"><a href="javascript:;">关闭当前标签页</a></dd>
                <dd layminna-event="closeOtherTabs"><a href="javascript:;">关闭其它标签页</a></dd>
                <dd layminna-event="closeAllTabs"><a href="javascript:;">关闭全部标签页</a></dd>
              </dl>
            </li>
          </ul>
        </div>
        <div class="layui-tab" lay-unauto lay-allowClose="true" lay-filter="layminna-layout-tabs">
          <ul class="layui-tab-title" id="LAY_minna_tabsheader">
            <li id="minna-homeurl-tablehead" lay-id="" lay-attr="" class="layui-this"><i class="layui-icon layui-icon-home"></i></li>
          </ul>
        </div>
      </div>

      <!-- 主体内容 -->
      <div class="layui-body" id="LAY_app_body">
        <div class="layminna-tabsbody-item layui-show">
          <iframe id="minna-homeurl-body" src="" frameborder="0" class="layminna-iframe"></iframe>
        </div>
      </div>

      <!-- 辅助元素，一般用于移动设备下遮罩 -->
      <div class="layminna-body-shade" layminna-event="shade"></div>
    </div>
  </div>

  <script src="./layui-minna/layui/layui.js"></script>
  <script>
    layui.config({
      base: './layui-minna/'
    }).extend({
      index: 'index'
    }).use('index');

    function _IsUrl(url) 
    {
      let match2 = /^((http|https):\/\/)+([\w\-])+/;
      let testVol = match2.test(url);
      return testVol;
    }
    
    layui.use(['index', 'element'], function () {
        var $ = layui.$
        , setting = layui.setting
        , minna = layui.minna
        , form = layui.form
        , router = layui.router()
        ,ele = layui.element;

        var systemName = "";

        var table = layui.data(layui.setting.tableName);
        var tokenNName = layui.setting.request.nickName;
        var tokenNValue = table[tokenNName];

        var tokenName = layui.setting.request.tokenName;
        var tokenValue = table[tokenName];

        var windowComeback = document.getElementById('window-comeback');
        windowComeback.addEventListener('click', function () {
        window.history.back(-1);      
        });
        
        var windowgonext = document.getElementById('window-gonext');
        windowgonext.addEventListener('click', function () {
        window.history.go(1);
        });

        if (tokenNValue) {
          var adminlevel;
          $("[name=omt-nickname]").text(tokenNValue);

          var urlgetme = setting.url +  "users/getme?MINNA_TOKEN="+tokenValue;
          var request = new XMLHttpRequest();
          request.open(
              "GET",
              urlgetme,
              true
          );
          request.setRequestHeader(
              "Content-Type",
              "application/x-www-form-urlencoded"
          );
          request.withCredentials = true;
          request.onreadystatechange = function () {
            if (request.readyState === 4) 
            {
              var objStr = JSON.parse(request.responseText);
              if (null!=objStr) 
              {
                if (0 == objStr.code)
                {
                }
                else
                {
                  layui.data(setting.tableName, {
                    key: setting.request.nickName
                    ,value: ""
                  });

                  layui.data(setting.tableName, {
                    key: setting.request.tokenName
                    ,value: ""
                  });

                  layer.msg('登入成功', {
                    offset: '15px'
                    ,icon: 1
                    ,time: 1000
                  }, function(){
                    location.href = 'login.html'; //后台主页
                  });
                }
              }
            }
          };
          request.send(null);
          
          minna.req({
            url: setting.url +  "users/getme"
            , data: {
            }
            , done: function (res) {
              
              adminlevel = res.data.adminlevel;
            }
          });

          var strPathCata = setting.url +  'getcatas';
          minna.req({
            url: strPathCata
            , data: {}
            , done: function (res) {
              if (0 == res.code)
              {
                var htmlTextCata = "";

                var strPathPlugin = setting.url +  'getplugins';
                minna.req({
                  url: strPathPlugin
                  , data: {}
                  , done: function (res1) {
                    if (0 == res1.code)
                    {     
                      for (var i=0; i<res.data.length; i++){
                        var cat = res.data[i];                     
                        htmlTextCata += '<li data-name="' + cat.name + '" class="layui-nav-item">';
                        htmlTextCata += '<a href="javascript:;" lay-tips="cata" lay-direction="2">';                    
                        htmlTextCata += '<i class="'+cat.icon+'"></i>';                    
                        htmlTextCata += '<cite>' + cat.title +  '</cite>';
                        htmlTextCata += '</a>';
                        htmlTextCata += '<dl class="layui-nav-child" id = ' + cat.id + '>';
          
                        for (var j=0; j<res1.data.length; j++)
                        {
                          var plg = res1.data[j];
                          var indx = plg.index;
                          var cat1 = plg.cata;

                          if (cat1 ==  cat.id)
                          {
                            // var ele1 = document.getElementById(cat);
                            // if (ele1)
                            {
                              var htmlText = "";

                              htmlText += '<dd data-name="plugin">';

                              if (indx && ""!=indx)
                              {
                                if(_IsUrl(indx))
                                {
                                  htmlText += '<a lay-href=' + indx + '>' + plg.title + '</a>';
                                }
                                else{
                                  htmlText += '<a lay-href="./' + plg.dir + '/' + indx + '">' + plg.title + '</a>';
                                }
                              }
                              else{
                                htmlText += '<a lay-href="./' + plg.dir + '/index.html">' + plg.title + '</a>';
                              }

                              htmlText += '</dd>';

                              htmlTextCata += htmlText; 
                            }  
                          }                                                                                                                                                                            
                        }

                        htmlTextCata += '</dl>';
                        htmlTextCata += '</li>';                                                                                                                                                                                                    
                      }

                      document.getElementById("cata").innerHTML = htmlTextCata;
                      ele.render("nav");
                    }
                  }
                });                   
              } 
            }
          });

          var strPath = setting.url +  'getsystem';
          minna.req({
            url: strPath
            , data: {}
            , done: function (res) {
              if (0 == res.code)
              {
                  var sys = res.data;

                  systemName = sys.name;

                  document.getElementById("minna-title").innerText = sys.title;
                  document.getElementById("minna-headtitle").innerText = sys.title;

                  if (sys.index && ""!=sys.index)
                  {
                    document.getElementById("minna-homeurl-sidmenulogo").attributes["lay-href"].value = sys.index;
                    document.getElementById("minna-homeurl-body").attributes["src"].value = sys.index;
                  }

                  ele.render("nav"); 
              }
            }
          });    
        }
        else {
          var loginhtml = "";
          if ("minna-mk" == systemName)
          {
            loginhtml = "login-minna.html";
          }
          else
          {
            loginhtml = "login.html";
          }

          location.href = setting.url +  loginhtml;
        }

        minna.events.logout = function(){
          //执行退出接口
          minna.req({
            url: setting.url +  'users/logout'
            ,type: 'get'
            ,data: {}
            ,done: function(res){ //这里要说明一下：done 是只有 response 的 code 正常才会执行。而 succese 则是只要 http 为 200 就会执行
              
              $("[name=omt-nickname]").text("");

              minna.exit(function(){
                layui.data(setting.tableName, {
                  key: setting.request.nickName
                  ,value: ""
                });
        
                layui.data(setting.tableName, {
                  key: setting.request.tokenName
                  ,value: ""
                });

                var loginhtml = "";
                if ("minna-mk" == systemName)
                {
                  loginhtml = "login-minna.html";
                }
                else
                {
                  loginhtml = "login.html";
                }
                
                location.href = setting.url +  loginhtml;
              });
            }
          });
        };
    });  

  </script>
</body>
</html>